//
//  String+Extension.swift
//  Weibo
//
//  Created by Albert Zhu on 16/10/14.
//  Copyright © 2016年 Albert Zhu. All rights reserved.
//

import Foundation

extension String {
    /// 快速生成沙盒路径
    ///
    /// - returns: 沙盒路径
    func az_appendDocumentDir() -> String{
        let path = NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, true)[0]
        let filePath = (path as NSString).appendingPathComponent(self)
        
        return filePath
    }
    
    /// 微博来源正则
    ///
    /// - returns: link: 连接, text: 来源
    func az_href() -> (link: String, text: String)? {
        
        let pattern = "<a href=\"(.*?)\".*?>(.*?)</a>"
        
        guard let regx = try? NSRegularExpression(pattern: pattern, options: []),
            let result = regx.firstMatch(in: self, options: [], range: NSRange(location: 0, length: characters.count)) else {return nil}
        
        let link = (self as NSString).substring(with: result.rangeAt(1))
        let text = (self as NSString).substring(with: result.rangeAt(2))
        
        debugPrint("\(link)---------\(text)")
        
        return (link, text)
    }

}
